package com.dal.manager;

import com.dal.constant.PaymentConstant;
import com.dal.db.bean.RechargeOrder;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import org.utils.RemoteUtil;

/**
 * 充值管理器
 * Created by fxf on 2018-2-9.
 */
public class PaymentManager {

    /**
     * 获取订单号
     *
     * @param playerId 玩家id
     * @param sid      服务器id
     * @param goodsId  商品id
     */
    public static String getOrderNo(int playerId, String sid, int goodsId) {
        RechargeOrder order = new RechargeOrder("", 0, sid, PaymentConstant.ORDER_STATE_NON_PAYMENT, 0f, "", "", goodsId, playerId, PaymentConstant.GOODS_NON_DELIVERY,"");
        order.insert();
        return order.getId();
    }

    /**
     * 充值成功，通知游戏服交付商品
     */
    public static void paymentNotice(RechargeOrder order) throws AbstractLogicModelException{
        order.setState(PaymentConstant.ORDER_STATE_ALREADY_PAID);
        order.update();

        boolean goodsDelivered = RemoteUtil.gameSync(order.getSid()).noticePaymentSuccess(order.getPlayerId(), order.getId(), order.getGoodsId());

        if (goodsDelivered) {
            order.setDeliveryState(PaymentConstant.GOODS_DELIVERED);
            order.update();
        }
    }
}
